<template>
  <div>
    <header class="bookxq">
      <div class="title">
        <router-link to class="left">
          <span class="ui-leftaw-2" @click="$router.back(-1)"></span>
          <!-- <span class="ui-leftaw-2"></span> -->
        </router-link>
        <div class="tit-top">
          <h3>登录页面</h3>
        </div>
      </div>
    </header>
    <loading v-if="isLoading" :msg="msg"></loading>
    <div class="frome">
      <div class="form">
        <div class="user">
          <input v-model="user.id" type="text" placeholder="请输入用户名或id" />
        </div>
        <div class="password">
          <input v-model="user.pwd" type="password" placeholder="请输入密码" />
        </div>
        <div class="newuser">
          <router-link to="/register">未注册账号，跳转注册</router-link>
        </div>
        <div class="post">
          <button @click="loginFunc">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Loading from "@/components/loading/longincg.vue";
export default {
  data() {
    return {
      user: {
        id: "",
        pwd: "",
      },
      isLoading: false,
      msg:''
    };
  },
  components: {
    Loading,
  },
  methods: {
    loginFunc() {
      if (this.user.id == "" || this.user.pwd == "") {
        this.msg='输入框不能为空面'
        this.isLoading = true;
        const that = this;
        setTimeout(function () {
          that.isLoading = false;
        }, 1200);
        return;
      } else {
        let datastr = localStorage.getItem("user");
        if (datastr) {
          let iddata = JSON.parse(datastr);
          console.log(iddata);
          if (
            this.user.id == iddata.id ||
            (this.user.id == iddata.name && this.user.pwd == iddata.pwd)
          ) {
            let token = {};
            token.user = iddata;
            token.bookid = {};
            token.cookic = {};
            console.log(token);
             localStorage.removeItem("user");
            localStorage.setItem("userdata", JSON.stringify(token));
            this.msg='登录成功，正在跳转用户界面'
            this.isLoading = true;
            const that = this;
            setTimeout(function () {
             that.isLoading = false;
              that.$router.push({ path: "/user" });
            }, 1200);
          } else {
            // console.log("账号不存在")
            this.msg='账号不存在'
            this.isLoading = true;
            const that = this;
        setTimeout(function () {
          that.isLoading = false;
        }, 1200);
          }
        } else {
          this.msg='请先注册账号'
            this.isLoading = true;
            const that = this;
        setTimeout(function () {
          that.isLoading = false;
        }, 1200);
        }
      }
    },
  },
};
</script>
<!-- https://apis.netstart.cn/yunyuedu/ -->
<style lang="scss">
@import url(../../static/css/reset.css);
.frome {
  // background: black;
  padding: 20px;
  .form {
    margin: 0 auto;
    width: 98%;
    color: #bdbdbd;
    // background-color: black;
    text-align: center;
    input {
      padding: 7px;
      width: 100%;
      height: 50px;
      line-height: 50px;
      box-sizing: border-box;
      border: 1px solid #bdbdbd;
      border-radius: 3px;
    }
    button {
      width: 90%;
      height: 40px;
      margin-top: 10px;
      box-sizing: border-box;
      background: #ebebe6;
      color: #aba499;
      border: 1px solid transparent;
      border-radius: 5px;
    }
    .newuser {
      margin-top: 10px;
      a {
        cursor: pointer;
        color: #bbb7b7;
      }
    }
  }
}
.bookxq {
  width: 100%;
  background-color: black;
  min-width: 362px;
  .title {
    margin: auto 1%;
    height: 44px;
    line-height: 44px;
    a.left {
      float: left;
      font-size: 14px;
      .ui-leftaw-2 {
        display: inline-block;
        margin-bottom: 3px;
        margin-left: 15px;
        vertical-align: middle;
        width: 10px;
        height: 18px;
        background: url(../../static/imgs/arrow-left.png);
        background-size: 100% 100%;
      }
    }
    .tit-top {
      padding: 0 5%;
      box-sizing: border-box;
      text-align: center;
      h3 {
        width: 30%;
        display: inline-block;
        color: white;
      }
    }
  }
}
</style>